package lambda;

import lambda.entity.User;

import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author LIBOFEI
 * @ClassName Test10
 * @description
 * @date 2021/11/30 14:43
 * @Version 1.0
 **/
public class Test10 {

    public static void main(String[] args) {
//        listCollect();
//        将两个字符数组合并成一个新的字符数组。
//        listFlatMap();
//        求Integer集合的元素之和、乘积和最大值。
//        listReduce();
//        求所有员工的工资之和和最高工资。
//        userListReduce();
//        统计信息
//        streamCount();
        //分组
        streamGroup();
        //接合(joining)
//        streamJoining();
        //reduce
//        reduce();
        //排序
//        sort();
        //提取/组合
//        test();
    }

    public static void test(){
        String[] arr1 = { "a", "b", "c", "d" };
        String[] arr2 = { "d", "e", "f", "g" };

        Stream<String> stream1 = Stream.of(arr1);
        Stream<String> stream2 = Stream.of(arr2);
        // concat:合并两个流 distinct：去重
        List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
        // limit：限制从流中获得前n个数据
        List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
        // skip：跳过前n个数据
        List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());

        System.out.println("流合并：" + newList);
        System.out.println("limit：" + collect);
        System.out.println("skip：" + collect2);
    }

    public static void sort(){
//        sorted()：自然排序，流中元素需实现Comparable接口
//        sorted(Comparator com)：Comparator排序器自定义排序
        List<User> userList = new ArrayList<User>();
        userList.add(new User(23, "male1"));
        userList.add(new User( 25, "male2"));
        userList.add(new User( 21, "female1"));
        userList.add(new User( 24, "female2"));
        userList.add(new User( 25, "male3"));
        userList.add(new User(26, "female3"));
// 按工资升序排序（自然排序）
        List<String> newList = userList.stream().sorted(Comparator.comparing(User::getId)).map(User::getName).collect(Collectors.toList());
        System.out.println("按id升序排序：" + newList);
//        // 按工资倒序排序
        List<String> newList2 = userList.stream().sorted(Comparator.comparing(User::getId).reversed()).map(User::getName).collect(Collectors.toList());
        System.out.println("按id降序排序：" + newList2);
//        // 先按工资再按年龄升序排序
        List<String> newList3 = userList.stream()
                .sorted(Comparator.comparing(User::getId).thenComparing(User::getName)).map(User::getName)
                .collect(Collectors.toList());
        System.out.println("先按工资再按名称升序排序：" + newList3);

//        // 先按工资再按年龄自定义排序（降序）
        List<String> newList4 = userList.stream().sorted((p1, p2) -> {
            if (p1.getId() == p2.getId()) {
                return p2.getName().compareTo(p1.getName());
            } else {
                return p2.getId() - p1.getId();
            }
        }).map(User::getName).collect(Collectors.toList());
        System.out.println("先按工资再按年龄自定义降序排序：" + newList4);
//
//
//
//
//
    }

    public static void reduce(){
        List<User> userList = new ArrayList<User>();
        userList.add(new User(23, "male1"));
        userList.add(new User( 25, "male2"));
        // 每个用户减去起征点后的id之和（这个例子并不严谨，但一时没想到好的例子）
        Integer sum = userList.stream().collect(Collectors.reducing(0, User::getId, (i, j) -> (i + j - 1)));
        System.out.println("员工扣税薪资总和：" + sum);

        // stream的reduce
        Optional<Integer> sum2 = userList.stream().map(User::getId).reduce(Integer::sum);
        System.out.println("员工薪资总和：" + sum2.get());
    }

    public static void streamJoining(){
        List<User> userList = new ArrayList<User>();
        userList.add(new User(23, "male1"));
        userList.add(new User( 25, "male2"));
        userList.add(new User( 21, "female1"));
        userList.add(new User( 24, "female2"));
        userList.add(new User( 25, "male3"));
        userList.add(new User(26, "female3"));

        String names = userList.stream().map(p -> p.getIdstr()).collect(Collectors.joining(","));
        System.out.println("所有员工的id：" + names);
        List<String> list = Arrays.asList("A", "B", "C");
        String string = list.stream().collect(Collectors.joining("-"));
        System.out.println("拼接后的字符串：" + string);
    }



    public static void streamGroup(){
        List<User> userList = new ArrayList<User>();
        userList.add(new User(23, "male1"));
        userList.add(new User( 25, "male2"));
        userList.add(new User( 21, "female1"));
        userList.add(new User( 24, "female2"));
        userList.add(new User( 25, "male3"));
        userList.add(new User(26, "female3"));
        // 将员工按id是否高于24分组
        Map<Boolean, List<User>> part = userList.stream().collect(Collectors.partitioningBy(x -> x.getId() > 8000));
        // 将员工按名称分组
        Map<String, List<User>> group = userList.stream().collect(Collectors.groupingBy(User::getName));
        // 将员工先按名称分组，再按id分组
        Map<String, Map<Integer, List<User>>> group2 = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.groupingBy(User::getId)));
        System.out.println("员工按薪资是否大于8000分组情况：" + part);
        System.out.println("员工按性别分组情况：" + group);
        System.out.println("员工按性别、地区：" + group2);

        List<Integer> integers = new ArrayList<>();
        integers.addAll(Arrays.asList(new Integer[]{1,2,3,4,5,7,7,6,8,9,5,4,6,4,4,4,5,5}));
        Map<Integer, List<Integer>> listMap = integers.stream().collect(Collectors.groupingBy(Function.identity()));

        System.out.println(listMap);
    }

    public static void streamCount(){
        List<User> userList = new ArrayList<User>();
        userList.add(new User(23, "male1"));
        userList.add(new User( 25, "male2"));
        userList.add(new User( 21, "female1"));
        userList.add(new User( 24, "female2"));
        userList.add(new User( 25, "male3"));
        userList.add(new User(26, "female3"));
        // 求总数
        Long count = userList.stream().collect(Collectors.counting());
        // 求平均id
        Double average = userList.stream().collect(Collectors.averagingDouble(User::getId));
        // 求最高id
        Optional<Integer> max = userList.stream().map(User::getId).collect(Collectors.maxBy(Integer::compare));
        // 求id之和
        Integer sum = userList.stream().collect(Collectors.summingInt(User::getId));
        // 一次性统计所有信息
        DoubleSummaryStatistics collect = userList.stream().collect(Collectors.summarizingDouble(User::getId));

        System.out.println("员工总数：" + count);
        System.out.println("员工最高id：" + max);
        System.out.println("员工平均工资：" + average);
        System.out.println("员工工资总和：" + sum);
        System.out.println("员工工资所有统计：" + collect);
    }

    public static void userListReduce(){
        List<User> userList = new ArrayList<User>();
        userList.add(new User(23, "male1"));
        userList.add(new User( 25, "male2"));
        userList.add(new User( 21, "female1"));
        userList.add(new User( 24, "female2"));
        userList.add(new User( 25, "male3"));
        userList.add(new User(26, "female3"));


        // 求id之和方式1：
        Optional<Integer> sumSalary = userList.stream().map(User::getId).reduce(Integer::sum);
        // 求id之和方式2：0是sum的初始值
        Integer sumSalary2 = userList.stream().reduce(0, (sum, p) -> sum += p.getId(),
                (sum1, sum2) -> sum1 + sum2);
//        // 求工资之和方式3：
        Integer sumSalary3 = userList.stream().reduce(0, (sum, p) -> sum += p.getId(), Integer::sum);

//        // 求最高工资方式1：
        Integer maxSalary = userList.stream().reduce(0, (max, p) -> max > p.getId() ? max : p.getId(),
                Integer::max);

        // 求最高工资方式2：
        Integer maxSalary2 = userList.stream().reduce(0, (max, p) -> max > p.getId() ? max : p.getId(),
                (max1, max2) -> max1 > max2 ? max1 : max2);
//
        System.out.println("工资之和：" + sumSalary.get() + "," + sumSalary2 + "," + sumSalary3);
        System.out.println("最高工资：" + maxSalary + "," + maxSalary2);
    }

    public static void listReduce(){
        List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4);
        // 求和方式1
        Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
        // 求和方式2
        Optional<Integer> sum2 = list.stream().reduce(Integer::sum);
        // 求和方式3
        Integer sum3 = list.stream().reduce(0, Integer::sum);
        // 求乘积
        Optional<Integer> product = list.stream().reduce((x, y) -> x * y);
        // 求最大值方式1
        Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y);
        // 求最大值写法2
        Integer max2 = list.stream().reduce(1, Integer::max);
        System.out.println("list求和：" + sum.get() + "," + sum2.get() + "," + sum3);
        System.out.println("list求积：" + product.get());
        System.out.println("list求和：" + max.get() + "," + max2);
    }

    public static void listFlatMap() {
        List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
        List<String> listNew = list.stream().flatMap(s -> {
            // 将每个元素转换成一个stream
            String[] split = s.split(",");
            Stream<String> s2 = Arrays.stream(split);
            return s2;
        }).collect(Collectors.toList());

        System.out.println("处理前的集合：" + list);
        System.out.println("处理后的集合：" + listNew);
    }

    public static void listCollect(){
        String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
        List<String> strList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
        List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
        List<Integer> intListNew = intList.stream().map(x -> x + 3).collect(Collectors.toList());
        System.out.println("原来：" + strArr.toString());
        System.out.println("每个元素大写：" + strList);
        System.out.println("原来：" + intList);
        System.out.println("每个元素+3：" + intListNew);
    }
}
